import {combineReducers, configureStore} from '@reduxjs/toolkit';
import counterReducer from './counter/counterSlice';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {persistStore, persistReducer} from 'redux-persist';

// 持久化配置
const persistConfig = {
  key: 'root', // 存储的键名
  storage: AsyncStorage, // 使用AsyncStorage作为存储引擎
  whitelist: ['counter'], // 仅持久化指定Reducer
  // blacklist: ['otherReducer'], // 可选：排除指定Reducer
};

// 合并所有reducer
const rootReducer = combineReducers({
  counter: counterReducer,
});

// 创建持久化的reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 创建store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware({serializableCheck: false}),
});

// 创建持久化存储实例
export const persistor = persistStore(store);
